package state.person.events;

import static org.junit.Assert.*;

import messages.ByteList;

import org.junit.Test;

import state.group.HallCall;
import state.group.events.EventType;
import state.person.Person;
import state.person.PersonType;

public class PersonRegisterEventTest {

	@Test
	public void testAppendToByteList()
	{
		Person p = new Person("TestPerson", PersonType.Dummy, 1, new HallCall((byte) 2, (byte) 3), (byte) 4);
		PersonRegisterEvent e = new PersonRegisterEvent(p);
		
		ByteList list = new ByteList();
		
		e.appendToByteList(list);
		
		byte[] out = list.toByteArray();
		
		assertEquals(20, out.length);
		
		byte eventId = EventType.PersonRegisterEvent.getIdentifier();
		assertEquals(eventId, out[0]);
		
		assertEquals('T', out[1]);
		assertEquals('e', out[2]);
		assertEquals('s', out[3]);
		assertEquals('t', out[4]);
		assertEquals('P', out[5]);
		assertEquals('e', out[6]);
		assertEquals('r', out[7]);
		assertEquals('s', out[8]);
		assertEquals('o', out[9]);
		assertEquals('n', out[10]);
		assertEquals(0, out[11]);
		
		byte id = PersonType.Dummy.getIdentifier();
		assertEquals(id, out[12]);
		
		assertEquals(2, out[13]);
		assertEquals(3, out[14]);
		assertEquals(4, out[15]);
		
		assertEquals(1, out[16]);
		assertEquals(0, out[17]);
		assertEquals(0, out[18]);
		assertEquals(0, out[19]);
		
	}
	
	@Test
	public void testParse()
	{
		byte id = EventType.PersonRegisterEvent.getIdentifier();
		byte[] parseTest = new byte[] {
			id,
			'H',
			'e',
			'l',
			'l',
			'o',
			0,
			PersonType.Dummy.getIdentifier(),
			1,
			2,
			3,
			9,
			0,
			0,
			0
		};
		
		PersonRegisterEvent e = new PersonRegisterEvent(parseTest, 0);
		
		assertEquals(15, e.getEventByteLength());
		assertEquals(EventType.PersonRegisterEvent, e.getEventType());
		assertEquals(e.getPersonId().toString(), "Hello");
		
		Person p = e.getPerson();
		assertEquals(1, p.getHallCall().getFloor());
		assertEquals(2, p.getHallCall().getDirection());
		assertEquals(3, p.getDestination());
		assertEquals(PersonType.Dummy, p.getType());
		assertEquals(9, p.getWeight());
	}
	
	@Test
	public void testCycle()
	{
		Person p = new Person("TestPerson", PersonType.Dummy, 1, new HallCall((byte) 1, (byte) 2), (byte) 3);
		PersonRegisterEvent e = new PersonRegisterEvent(p);
		
		ByteList list = new ByteList();
		e.appendToByteList(list);
		
		byte[] out = list.toByteArray();
		
		e = new PersonRegisterEvent(out, 0);
		
		assertEquals(p, e.getPerson());
	}
	
	@Test
	public void testEventType()
	{
		Person p = new Person("hi", PersonType.Dummy, 0, new HallCall((byte) 0, (byte) 0), (byte) 0);
		PersonRegisterEvent e = new PersonRegisterEvent(p);
		
		assertEquals(EventType.PersonRegisterEvent, e.getEventType());
	}

}
